/**
 * @ author: 汪孟苹
 */
define(["jquery","bootstrap","bbcare","comm/datatable/datatable"],function($,bootstrap,$bb,$datatable){
    var servers = {};
    var URL = $bb.baseUrl("followapp");

    /**
     * 入参
     */
    var inParams = function(args){
        var params = $bb.getPageParams();
        var arg = {
            "args": JSON.stringify(args),
            "queueId":params.queueId,
            "roleType":"1", // 患者自助
            "hisNo":$.trim( $("#datatableForm input[name=hisNo]").val() ),
            "userName":$.trim( $("#datatableForm input[name=userName]").val() ),
            "phoneNo":$.trim( $("#datatableForm input[name=phoneNo]").val() ),
        }
        if( $("#datatableForm input[name=ageFrom]").length > 0 && $("#datatableForm input[name=ageTo]").length > 0 ){
            var ageFrom = $("#datatableForm input[name=ageFrom]").val();
            var ageTo = $("#datatableForm input[name=ageTo]").val();
            ageFrom = ageFrom ? ageFrom * 12 : ageFrom;
            ageTo = ageTo ? ageTo * 12 : ageTo;
            $.extend(arg,{"ageFrom":ageFrom,"ageTo":ageTo});
        }
        if( params.kind == "1" ){
            var arg1 = {
                "flag":"1", // 儿童
                "gender": $("#datatableForm input[name=gender]:checked").val(),
                "beginDate":$("#datatableForm input[name=beginDate]").val(),
                "endDate":$("#datatableForm input[name=endDate]").val(),
            };
            $.extend(arg,arg1);
        }else if( params.kind == "2" ){
            var arg2 = {
                "flag":"2", // 孕前
                "beginBMI":$.trim( $("#datatableForm input[name=beginBMI]").val() ),
                "endBMI":$.trim( $("#datatableForm input[name=endBMI]").val() ),
                "pregnancyStatus":$("#datatableForm select[name=pregnancyStatus]").val(),
                "weight":$("#datatableForm select[name=weightSituation]").val(),
            };
            $.extend(arg,arg2);
        }else if( params.kind == "3" ){
            // 孕周
            var preFromWeek = $("#datatableForm input[name=preFrom-week]").val();
            var preFromDay = $("#datatableForm input[name=preFrom-day]").val();
            var preToWeek = $("#datatableForm input[name=preTo-week]").val();
            var preToDay = $("#datatableForm input[name=preTo-day]").val();
            var preDayFrom, preDayTo;
            if( preFromWeek != "" || preFromDay != "" ){
                preDayFrom = parseInt(preFromWeek*7) + parseInt(preFromDay*1);
            }else{
                preDayFrom = null;
            }
            if( preToWeek != "" || preToDay !="" ){
                preDayTo = parseInt(preToWeek*7) + parseInt(preToDay*1);
            }else{
                preDayTo = null;
            }
            var arg3 = {
                "flag":"3", // 孕期
                "parturientStatus":$("#datatableForm select[name=parturientStatus]").val(),
                "preDayFrom":preDayFrom,
                "preDayTo":preDayTo,
                "beginExpectedDay":$("#datatableForm input[name=beginExpectedDay]").val(),
                "endExpectedDay":$("#datatableForm input[name=endExpectedDay]").val(),
            };
            $.extend(arg,arg3);
        }
        var data = JSON.stringify({
            data: arg
        });
        return data;
    }
    /**
     * 出参
     */
    var outParams = function(data){
        return data.rows;
    }
    /**
     * columns
     */
    var columnsHisNo = function(e){
        return e.hisNo ? e.hisNo : e.hisNo1 ? e.hisNo1 : e.hisNo2 ? e.hisNo2 : "-";
    }
    var columnsGender = function(e){
        return e.gender == "F" ? "女" : e.gender == "M" ? "男" : "";
    }
    var columnsPregnancyStatus = function(e){
        //return e.pregnancyStatus == "1" ? "备孕中" : e.pregnancyStatus == "2" ? "备孕成功" : "";
        return e.expectedDay ? "备孕成功" : "备孕中";
    }
    var columnsWeightSituation = function(e){
        return e.preBmi ? e.preBmi < 18.5 ? "低体重" : e.preBmi < 24.9 ? "标准体重" : e.preBmi < 29.9 ? "超重" : "肥胖" : "";
    }
    var columnsStatus = function(e){
        var str;
        if( e.deliveryStatus == 1 ){ // 已分娩
            str = e.deliveryDay ? e.deliveryDay <= 56 ? "产褥期" : "产后期" : null;
        }else if( e.deliveryStatus == 2 ){ // 未分娩
            str = e.pregnancyDay ? e.pregnancyDay <= 91 ? "孕早期" : e.pregnancyDay <= 196 ? "孕中期" : "孕晚期" : null;
        }
        return str;
    }
    var columnsData = function(){
        var params = $bb.getPageParams();
        if( params.kind == "1" ){
            var columnsArr = [
                {"data":columnsHisNo},
                {"data":'userName'},
                {"data":columnsGender},
                {"data":'phoneNo'},
                {"data":'birthday'},
                {
                    "data": null,
                    "className":"operate"
                }
            ];
        }else  if( params.kind == "2" ){ // 未完成（字段没有） 待处理
            var columnsArr = [
                {"data":columnsHisNo},
                {"data":'userName'},
                {"data":'phoneNo'},
                {"data":'age'},
                {"data":columnsPregnancyStatus},
                {"data":'preBmi'},
                {"data":columnsWeightSituation},
                {
                    "data": null,
                    "className":"operate"
                }
            ];
        }else  if( params.kind == "3" ){ // 未完成（字段没有） 待处理
            var columnsArr = [
                {"data":columnsHisNo},
                {"data":'userName'},
                {"data":'phoneNo'},
                {"data":'age'},
                {"data":columnsStatus},
                {"data":'pregnancyWeek'},
                {"data":'deliveryDay'},
                {
                    "data": null,
                    "className":"operate"
                }
            ];
        }
        return columnsArr;
    }
    /**
     * 行绘制回调函数
     */
    var rowCallback = function(row, data, index) {
        // 操作
        var h = '<div class="operate-box"><i class="fa fa-eye color-mint" data-toggle="tooltip" data-placement="top" title="查看"></i></div>';
        $('td.operate', row).html(h);
        $("td.operate .operate-box i.color-mint",row).unbind("click");
        $("td.operate .operate-box i.color-mint",row).click(function(){
            var params = $bb.getPageParams();
            var arg = { "info":data };
            if( params.kind == "1" ){
                $bb.open(params,"lookBtn","bch_selfRecord",arg);
            }else if( params.kind == "2" ){
                $bb.open(params,"lookBtn","bch_progestationSelfRecord",arg);
            }else if( params.kind == "3" ){
                $bb.open(params,"lookBtn","bch_pregnancySelfRecord",arg);
            }
        });
    };
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function(setting,json){
        var id = $(".layout-bottom table").attr("id");
        $("#" + id).prev("div.row").remove();
        // 搜索 
        $("#serchBtn").click(function(){
            $datatable.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatable.table.api().ajax.params();   //获取dt请求参数 
        });
        // 初始化所有工具提示
        $(function(){$("[data-toggle='tooltip']").tooltip();});
    }
    /**
     * 获取datatable里的options
     */
    servers.getOptions = function(){
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "ctrl/selfManage/patientQueueList?sessionId=" + sessionId,
                "data": inParams,
                "dataSrc": outParams
            },
            "columns": columnsData(),
            "rowCallback": rowCallback,
            "initComplete": initComplete,
        };
        return options;
    }

    return servers;
});